home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / getopt.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  3KB  |  103 lines

  1. /*
  2. **    @(#)getopt.c    2.2 (smail) 1/26/87
  3. */
  4.  
  5. /*
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9.  * directly from AT&T.  The people there assure me that it is indeed
  10.  * in the public domain.
  11.  * 
  12.  * There is no manual page.  That is because the one they gave out at
  13.  * UNIFORUM was slightly different from the current System V Release 2
  14.  * manual page.  The difference apparently involved a note about the
  15.  * famous rules 5 and 6, recommending using white space between an option
  16.  * and its first argument, and not grouping options that have arguments.
  17.  * Getopt itself is currently lenient about both of these things White
  18.  * space is allowed, but not mandatory, and the last option in a group can
  19.  * have an argument.  That particular version of the man page evidently
  20.  * has no official existence, and my source at AT&T did not send a copy.
  21.  * The current SVR2 man page reflects the actual behavor of this getopt.
  22.  * However, I am not about to post a copy of anything licensed by AT&T.
  23.  */
  24.  
  25. #include "global.h"
  26. #define index strchr
  27. #ifdef BSD
  28. #include <strings.h>
  29. #else
  30. #include <string.h>
  31. #endif
  32.  
  33. #ifdef    __TURBOC__
  34. #include <io.h>        /* added by KA9Q for Turbo-C */
  35. #endif
  36. #ifdef    AMIGA
  37. #include <fcntl.h>
  38. #endif
  39.  
  40. /*LINTLIBRARY*/
  41. #ifndef    NULL
  42. #define NULL    0
  43. #endif
  44. #define EOF    (-1)
  45. #define ERR(s, c)    if(opterr){\
  46.     extern int write __ARGS((int,void*,unsigned));\
  47.     char errbuf[2];\
  48.     errbuf[0] = c; errbuf[1] = '\n';\
  49.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  50.     (void) write(2, s, (unsigned)strlen(s));\
  51.     (void) write(2, errbuf, 2);}
  52.  
  53. int    opterr = 1;
  54. int    optind = 1;
  55. int    optopt;
  56. char    *optarg;
  57.  
  58. int
  59. getopt(argc, argv, opts)
  60. int    argc;
  61. char    **argv, *opts;
  62. {
  63.     static int sp = 1;
  64.     register int c;
  65.     register char *cp;
  66.  
  67.     if(sp == 1)
  68.         if(optind >= argc ||
  69.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  70.             return(EOF);
  71.         else if(strcmp(argv[optind], "--") == 0) {
  72.             optind++;
  73.             return(EOF);
  74.         }
  75.     optopt = c = argv[optind][sp];
  76.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  77.         ERR(": illegal option -- ", c);
  78.         if(argv[optind][++sp] == '\0') {
  79.             optind++;
  80.             sp = 1;
  81.         }
  82.         return('?');
  83.     }
  84.     if(*++cp == ':') {
  85.         if(argv[optind][sp+1] != '\0')
  86.             optarg = &argv[optind++][sp+1];
  87.         else if(++optind >= argc) {
  88.             ERR(": option requires an argument -- ", c);
  89.             sp = 1;
  90.             return('?');
  91.         } else
  92.             optarg = argv[optind++];
  93.         sp = 1;
  94.     } else {
  95.         if(argv[optind][++sp] == '\0') {
  96.             sp = 1;
  97.             optind++;
  98.         }
  99.         optarg = NULL;
  100.     }
  101.     return(c);
  102. }
  103.